package com.pospi.xstore.ui.caigou;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.maning.library.MClearEditText;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.pospi.xstore.R;
import com.pospi.xstore.dto.KcBill;
import com.pospi.xstore.dto.StoreBeen;
import com.pospi.xstore.http.LoadServer;
import com.pospi.xstore.ui.base.BaseActivity;
import com.pospi.xstore.util.Api;
import com.pospi.xstore.util.Constant;
import com.pospi.xstore.util.SaveOrderInfo;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

//库存查询

public class InventorySearchActivity extends BaseActivity {
    @Bind(R.id.et_search)
    MClearEditText et_search;
    @Bind(R.id.ll_title)
    LinearLayout ll_title;
    @Bind(R.id.kc_list)
    ListView kc_list;
    @Bind(R.id.tv_zkcsl)
    TextView tv_zkcsl;
    @Bind(R.id.tv_zkcje)
    TextView tv_zkcje;


    private List<KcBill> kcBills = new ArrayList<>();
    private MyAdapter adapter;
    private String count;
    private String je;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_inventory_list);
        ButterKnife.bind(this);
        setTitle("库存查询");
        ll_title.setVisibility(View.GONE);
        setMenuImage(R.drawable.icon_search_white);
        adapter = new MyAdapter();
        kc_list.setAdapter(adapter);

    }

    @Override
    public void onMenuClick(View view) {
        super.onMenuClick(view);
        startActivity(InventorySearchDetailActivity.class);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        List<KcBill> list = (List<KcBill>) intent.getSerializableExtra("cgdd_info");
        count = intent.getStringExtra("count");
        je = intent.getStringExtra("je");
        kcBills.clear();
        kcBills.addAll(list);
        if (kcBills.size() > 0) {
            ll_title.setVisibility(View.VISIBLE);
            tv_zkcsl.setText(count);
            tv_zkcje.setText(je);
        } else {
            ll_title.setVisibility(View.GONE);
            Toast.makeText(this, "未查询到数据", Toast.LENGTH_SHORT).show();
        }
        adapter.notifyDataSetChanged();

    }

    @OnClick({R.id.iv_search_xz})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search_xz://搜索
                searchBill();
                break;

        }
    }
    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return kcBills.size();
        }

        @Override
        public Object getItem(int i) {
            return kcBills.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder ;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(InventorySearchActivity.this).inflate(R.layout.kc_bill_item, null);
                holder.name = view.findViewById(R.id.name);
                holder.tv_jj = view.findViewById(R.id.tv_jj);
                holder.tv_lsj = view.findViewById(R.id.tv_lsj);
                holder.tv_pfj = view.findViewById(R.id.tv_pfj);
                holder.tv_sl = view.findViewById(R.id.tv_sl);
                holder.tv_js = view.findViewById(R.id.tv_js);
                holder.tv_kcje = view.findViewById(R.id.tv_kcje);
                holder.code = view.findViewById(R.id.code);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            KcBill bill = kcBills.get(i);
            holder.name.setText(bill.getName());
            holder.code.setText(bill.getBarcode());
            holder.tv_jj.setText("进价：" + bill.getJj());
            holder.tv_lsj.setText("零售价：" + bill.getLsj());
            holder.tv_pfj.setText("批发价：" + bill.getPfj());
            holder.tv_sl.setText("库存数量：" + bill.getKcsl());
            holder.tv_js.setText("库存件数：" + bill.getKcjs());
            holder.tv_kcje.setText("库存金额：" + bill.getJjje());
            return view;
        }
        class Holder{
            TextView name;
            TextView tv_jj;
            TextView tv_lsj;
            TextView tv_pfj;
            TextView tv_sl;
            TextView tv_js;
            TextView tv_kcje;
            TextView code;

        }
    }
    List<KcBill> bills = new ArrayList<>();
    private void searchBill() {
        showDialog();
        RequestParams params = new RequestParams();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "report.mkcbb");
        params.put("fun", "find_spkchz");

//        params.put("page", "");
//        params.put("limit", "");


        JSONObject object = new JSONObject();
        try {

            object.put("filter", et_search.getText().toString().trim());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        Log.i("login",params.toString());
        LoadServer.getConnect(getApplicationContext(), Api.LOGIN, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.i("login", new String(bytes));
                try {
                    JSONObject jsonObject = new JSONObject(new String(bytes));
                    JSONArray result = jsonObject.getJSONArray("result");
                    Log.i("login", jsonObject.getString("errMsg"));
                    count = jsonObject.getString("count");
                    je = jsonObject.getJSONObject("sumrow").getString("jjje");
                    if (result.length() > 1) {
                        for (int j=0;j<result.length()-1;j++) {
                            JSONObject data = result.getJSONObject(j);
                            KcBill kcBill = new KcBill();
                            kcBill.setName(data.getString("name"));
                            kcBill.setBarcode(data.getString("barcode"));
                            kcBill.setJj(data.getString("jj"));
                            kcBill.setLsj(data.getString("lsj"));
                            kcBill.setPfj(data.getString("pfj"));

                            kcBill.setKcsl(data.getString("kcsl"));
                            kcBill.setKcjs(data.getString("kcjs"));
                            kcBill.setJjje(data.getString("jjje"));
                            bills.add(kcBill);
                        }
                    }
                    Message message = Message.obtain();
                    message.what = 1;
                    handler.sendMessage(message);
                } catch (JSONException e) {

                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Message message = Message.obtain();
                message.what = 3;
                handler.sendMessage(message);
            }
        });
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    close();
                    kcBills.clear();
                    kcBills.addAll(bills);
                    if (kcBills.size() > 0) {
                        ll_title.setVisibility(View.VISIBLE);
                        tv_zkcsl.setText(count);
                        tv_zkcje.setText(je);
                    } else {
                        ll_title.setVisibility(View.GONE);
                        Toast.makeText(InventorySearchActivity.this, "未查询到数据", Toast.LENGTH_SHORT).show();
                    }
                    adapter.notifyDataSetChanged();
                    break;
            }

        }
    };

}
